package com.heima.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.gson.JsonObject;
import com.heima.mapper.ReportMapper;
import com.heima.pojo.OrderTotalMoney;
import com.heima.pojo.dto.PartnerCollectDto;
import com.heima.pojo.vo.*;
import com.heima.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;



    /**
     * 查询当月订单数
     * @param start
     * @param end
     * @return
     */
    @Override
    public Integer monthOrderCount(Date start, Date end) {
        return reportMapper.monthOrderCount(start,end);
    }

    /**
     * 统计当月销售额
     * @param first
     * @param last
     * @return
     */
    @Override
    public Long monthOrderAmount(Date first, Date last) {
        return reportMapper.monthOrderAmount(first,last);
    }

    @Override
    public AmountCollectVo AmountCollectVo(Integer type, Date first, Date last) {
        AmountCollectVo amountCollectVo = new AmountCollectVo();
        List<AmountVo> amountVos = reportMapper.queryAmount(first,last);
        log.info("mapper查询的结果:{}",amountVos);
        List<String> dates = amountVos.stream().map(AmountVo::getDate).toList();
        log.info("从结果中取出的日期:{}",dates);
        List<Integer> amounts = amountVos.stream().map(AmountVo::getAmount).toList();
        log.info("取出的金额:{}",amounts);
        amountCollectVo.setXAxis(dates);
        amountCollectVo.setSeries(amounts);
        return amountCollectVo;
    }


    /**
     * 销售量分布
     */
    @Override
    public AmountCollectVo regionCollect(Date first, Date last) {
        List<RegionCollectVo> regionCollectVos = reportMapper.regionCollect(first,last);
        log.info("mapper中拿到的regionCollectVos:{}",regionCollectVos);
        AmountCollectVo amountCollectVo = new AmountCollectVo();
        List<String> regionNames = regionCollectVos.stream().map(RegionCollectVo::getRegionName).toList();
        log.info("regionCollectVos中取出的names:{}",regionNames);
        List<Integer> amounts = regionCollectVos.stream().map(RegionCollectVo::getAmount).toList();
        log.info("regionCollectVos中取出的amounts:{}",amounts);
        amountCollectVo.setSeries(amounts);
        amountCollectVo.setXAxis(regionNames);
        return amountCollectVo;
    }

    /**
     * 获取一定日期范围之内的合作商分成汇总数据
     * @param dto
     * @return
     */
    @Override
    public PartnerCollectPageVo partnerCollect(PartnerCollectDto dto) {
        dto.check();
        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());
        Page<PartnerCollectVo> partnerCollectVos = reportMapper.partnerCollect(dto);
        log.info("从mapper中获取到的partnerCollectVos为:{}",partnerCollectVos);
        PartnerCollectPageVo pageVo = new PartnerCollectPageVo();
        OrderTotalMoney orderTotalMoney = new OrderTotalMoney();
        partnerCollectVos.forEach(partnerCollectVo -> {
            partnerCollectVo.setOrderTotalMoney(partnerCollectVo.getOrderCount());
        });
        Integer totalCount = 0,totalBill = 0;
        for (PartnerCollectVo partnerCollectVo : partnerCollectVos) {
            totalCount += partnerCollectVo.getOrderCount();
            totalBill += partnerCollectVo.getTotalBill();
        }
        orderTotalMoney.setTotalBill(totalBill);
        orderTotalMoney.setOrderCount(totalCount);
        pageVo.setPageIndex(dto.getPageIndex());
        pageVo.setPageSize(dto.getPageSize());
        pageVo.setTotalPage(partnerCollectVos.getPages());
        pageVo.setTotalCount(partnerCollectVos.getTotal());
        pageVo.setCurrentPageRecords(partnerCollectVos);
        pageVo.setOrderTotalMoney(orderTotalMoney);
        return pageVo;
    }


    /**
     * 获取一定时间范围之内的分成总数
     */
    @Override
    public Integer getTotalBill(Date start, Date end, Long id) {
        Integer totalBill = reportMapper.getTotalBill(start, end, id);
        log.info("mapper中返回的totalBill:{}",totalBill);
        return totalBill;
    }

}
